package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import com.byxm.wlgw.bean.DdspxxBean;
import com.byxm.wlgw.bean.GwcxxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.DdspxxQuery;

public class DdspxxService {
	private static final String BUNDLE_NAME="init";
	private static final ResourceBundle RESOURCE_BUNDLE=ResourceBundle.getBundle(BUNDLE_NAME);
	private String imgServiceUrl=RESOURCE_BUNDLE.getString("imgservice.server.url");
	
	DataBaseAccess dba=null;
	DdspxxQuery ddspxxQuery=new DdspxxQuery();
	
	
	public List<DdspxxBean> findDdspxx(String ddxxid) throws Exception{
		ResultSet rs=null;
		List<DdspxxBean> lstDdspxx=new ArrayList<DdspxxBean>();
		try {
			dba=new DataBaseAccess();
			rs=dba.executeQuery(ddspxxQuery.findDdspxx(ddxxid));
			while(rs.next()){
				DdspxxBean bean=new DdspxxBean();
				bean.setDdspxxid(rs.getInt("ddspxxid"));
				bean.setDdxxid(rs.getString("ddxxid"));
				bean.setSpxxid(rs.getInt("spxxid"));
				bean.setSpsl(rs.getInt("spsl"));
				bean.setSpyj(rs.getFloat("spyj"));
				bean.setSpsj(rs.getFloat("spsj"));
				bean.setSphh(rs.getString("sphh"));
				bean.setSpppm(rs.getString("spppm"));
				bean.setSplbm(rs.getString("splbm"));
				bean.setSpxhnr(rs.getString("spxhnr"));
				lstDdspxx.add(bean);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		
		return lstDdspxx;
	}
	/**
	 * 插入多条信息
	 * @throws Exception 
	 */
	public Boolean insertDdspxx(String ddxxid,List<GwcxxBean> lstGwcxx) throws Exception{
		int flag=0;
		
		String sql=ddspxxQuery.getInsertDdxpxxSql(ddxxid, lstGwcxx);
		try {
			dba=new DataBaseAccess();
			flag=dba.executeUpdate(sql);
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
				dba.releaseConnect();
		}
		
		if(flag>0){
			return true;
		}else{
			return false;
		}
		
	}
	/**
     * 获取订单商品信息
     * @return
     * @throws Exception
     */
    public List<DdspxxBean> findDdspxxlst(int yhxxid) throws Exception{
    	List<DdspxxBean> lstDdspxx=new ArrayList<DdspxxBean>();
    	ResultSet rs=null;
    	PreparedStatement ps=null;
    	try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(ddspxxQuery.findDdspxxSql());
			ps.setInt(1, yhxxid);
			rs=ps.executeQuery();
			while(rs.next()){
				DdspxxBean ddspxxBean=new DdspxxBean();
				ddspxxBean.setSptp(imgServiceUrl+rs.getString("sptp"));
				ddspxxBean.setSpmc(rs.getString("spmc"));
				ddspxxBean.setSpsj(rs.getFloat("spsj"));
				ddspxxBean.setShrxm(rs.getString("shrxm"));
				ddspxxBean.setJyzt(rs.getInt("jyzt"));
				ddspxxBean.setDdxxid(rs.getString("ddxxid"));
				lstDdspxx.add(ddspxxBean);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
    	return lstDdspxx;
    }
}
